package model.jgap;

import model.market.MarketInformation;
import model.util.SharpeCalc;

import org.jgap.FitnessFunction;
import org.jgap.Gene;
import org.jgap.IChromosome;

//AMLOW: have a fitness function that assesses benchmark risk and all that.
//AMLOW: anything more complicated.. limit dependence on one asset for example?
public class PortfolioSharpeFitnessFunction extends FitnessFunction{

	MarketInformation info;
	double[] expectedExcessAssetReturns;
	double indexVariance;
	double[] assetNonSpecificVariance;
	
	
	public PortfolioSharpeFitnessFunction(MarketInformation info, double[] expectedExcessReturns,double indexVariance,double[] assetNonSpecificVariance){
		this.info=info;
		this.expectedExcessAssetReturns=expectedExcessReturns;
		this.indexVariance=indexVariance;
		this.assetNonSpecificVariance=assetNonSpecificVariance;
		
	}
	
	protected double evaluate(IChromosome a_subject) {
		// AM find variance and expected excess return using betas and alphas. fitness function pays out on max sharpe ratio.
		Gene[] genes = a_subject.getGenes();
		double[] weightings = new double[genes.length];
		
		for(int i=0;i<genes.length;i++){
			Gene gene= genes[i];
			weightings[i]= (Double)gene.getAllele();
		}
		
		double sharpeRatio = SharpeCalc.calculateSharpeRatio(weightings,expectedExcessAssetReturns,info,indexVariance,assetNonSpecificVariance);
	
		if(sharpeRatio<0){
			sharpeRatio=0;
		}
		return sharpeRatio;
	}

	
	

}
